package domain

import (
	"gluenet/internal/glue-srvp/domain/aggregate"
	"gluenet/internal/glue-srvp/domain/vo"
	"gluenet/internal/pkg/infra"
	"gluenet/internal/pkg/infra/prepare"
)

func init() {
	prepare.RegisterService(func() {
		tag := NewTagDomain()
		prepare.InjectDependency(func() *Tag {
			return tag
		})
	})
}

type Tag struct {
	TagFactory *aggregate.TagFactory
}

func NewTagDomain() *Tag {
	domain := &Tag{}
	prepare.FetchDependency(&domain.TagFactory)
	return domain
}

func (r *Tag) FindTagDetail(auth infra.AuthInfo, query map[string]interface{}) (interface{}, error) {
	//tags, err := r.TagFactory.FindTagDetail(auth,query)
	//if err != nil {
	//	return nil, err
	//}
	//var tagRes = make([]interface{}, len(tags))
	//for index, tag := range tags {
	//	tagRes[index] = tag.ShowInfo()
	//}
	return r.TagFactory.FindTagDetail(auth, query)
}

func (r *Tag) FindGroup(auth infra.AuthInfo, query map[string]interface{}) (interface{}, error) {
	//tagGrp, err := r.TagFactory.FindGroup(auth,query)
	//if err != nil {
	//	return nil, err
	//}
	//var tagGrpRes = make([]interface{}, len(tagGrp))
	//for index, grp := range tagGrp {
	//	tagGrpRes[index] = grp.ShowInfo()
	//}
	return r.TagFactory.FindGroup(auth, query)
}

// Create 创建Tag
func (r *Tag) CreateTag(auth infra.AuthInfo, req vo.CreateTagReq) (interface{}, error) {
	//tagRes, err := r.TagFactory.CreateTag(auth,req)
	//if err != nil {
	//	return nil, err
	//}
	return r.TagFactory.CreateTag(auth, req)
}

// Update 更新Tag
func (r *Tag) UpdateTag(auth infra.AuthInfo, req vo.UpdateTagReq) (interface{}, error) {
	//_, err := r.TagFactory.UpdateTag(req)
	//if err != nil {
	//	return nil, err
	//}
	return r.TagFactory.UpdateTag(auth, req)
}

// Create 创建Tag Group
func (r *Tag) CreateTagGroup(auth infra.AuthInfo, req vo.CreateTagGroupReq) (interface{}, error) {
	//tagGrp, err := r.TagFactory.CreateTagGroup(req)
	//if err != nil {
	//	return nil, err
	//}
	//tagGrpE := tagGrp.(*entity.TagGroup)
	//reqUser := vo.CreateTagGroupUserReq{ID: tagGrpE.Id, UserID: user}
	//_, err = r.TagFactory.CreateTagGroupUser(reqUser)
	//if err != nil {
	//	return nil, err
	//}
	//
	//tagGrpRes := tagGrp.ShowInfo()
	return r.TagFactory.CreateTagGroup(auth, req)
}

// Update 更新Tag Group
func (r *Tag) UpdateTagGroup(auth infra.AuthInfo, req vo.UpdateTagGroupReq) (interface{}, error) {
	//tag, err := r.TagFactory.UpdateTagGroup(req)
	//if err != nil {
	//	return nil, err
	//}
	//var tagRes interface{}
	//tagRes = tag.ShowInfo()
	return r.TagFactory.UpdateTagGroup(auth, req)
}

// Delete 删除Tag
func (r *Tag) DeleteTag(auth infra.AuthInfo, tagID int64) (interface{}, error) {
	//id, err := strconv.ParseInt(tagID, 10, 64)
	//if err != nil {
	//	return nil, err
	//}
	obj, err := r.TagFactory.DelTag(auth, tagID)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

// Delete 删除Tag Group
func (r *Tag) DeleteTagGroup(auth infra.AuthInfo, tagGrpID int64) (infra.Entity, error) {
	//id, err := strconv.ParseInt(tagGrpID, 10, 64)
	//if err != nil {
	//	return nil, err
	//}
	obj, err := r.TagFactory.DelTagGroup(auth, tagGrpID)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

// 打Tag
func (r *Tag) CreateTagObj(req vo.TagObj) (interface{}, error) {
	tagObjRes, err := r.TagFactory.MarkTag(req)
	if err != nil {
		return nil, err
	}
	return tagObjRes, nil
}

// 解除Tag
func (r *Tag) UnTagObj(req vo.TagObj) (infra.Entity, error) {
	obj, err := r.TagFactory.UnMarkTag(req)
	if err != nil {
		return nil, err
	}
	return obj, nil
}

func (r *Tag) FindTagObj(auth infra.AuthInfo, value string) (interface{}, error) {
	tagObjs, err := r.TagFactory.FindTagObjs(auth, value)
	if err != nil {
		return nil, err
	}

	return tagObjs, nil
}

func (r *Tag) FindAllTagObj(auth infra.AuthInfo) (interface{}, error) {
	tagObjs, err := r.TagFactory.FindAllTagObjs(auth)
	if err != nil {
		return nil, err
	}

	return tagObjs, nil
}
